Repository navigation
Conversation
1e89645 to
d060937
Compare
|
Claude Code says GM can't overrun its buffers when darktable raises the count. In the future, G'MIC 3.x and 4.x. could cause problems -- see end of report. Also, 3 new (unrelated) bugs were found in XTrans-related code. I'll file those separately. GM's init (magick/resource.c:676-689) does two things with threads. It calls omp_set_num_threads() on the calling thread, using OMP_NUM_THREADS or the CPU count, and it stores that number as its ThreadsResource limit. It allocates nothing Every per-thread array in GM is sized at the moment it is allocated, from the calling thread's omp_get_max_threads():
In Pascal's example (GM picks 2, darktable restores 16), the restore runs inside dt_init(), before any GM image exists. Every image GM creates afterwards is sized for 16, and its parallel loops run on the same thread with 16 threads. The stored "2" is only read in two places:
GM does have one real constraint: an image must not be processed by more threads than existed when it was created. darktable creates, reads and destroys each GM image inside one function, with no count change in between. On "fragile" The earlier reply said "Codex read the GM sources", which invites the "we depend on GM internals" objection. The stronger argument doesn't need a GM audit:
Separate problem found while checking (not reproduced) G'MIC 3.x and 4.x call omp_set_num_threads(nb_cpus) every time a gmic instance is constructed, unless OMP_NUM_THREADS is set (gmic-v.4.0.5/src/gmic.cpp:3603, reached from :4183). darktable constructs one in lut3d for compressed LUTs (src/iop/lut3dgmic.cpp:48, :105). With -t below the CPU count, that would reset the count on the calling thread (a pipe worker, or darktable-cli's main thread) in the middle of a session. The reset in dt_init() can't cover that. The installed G'MIC is 2.9.4, which doesn't do this, so I couldn't reproduce it. This is the genuinely fragile part, and adding num_threads(dt_get_num_threads()) to DT_OMP_FOR would fix it. It belongs in its own issue, not this PR. |
|
Also: once #22456 and #22457 are fixed, a next fix can be added to allocate buffers based on the thread count, until that changing the allocation would change output. Over to Claude:
|
|
@kofa73 : So if I understand correctly this works with current GM but will fail with GM > 3.x. That's not a nice future :) What would be your proposal on this? If we still want to merge this PR I would like to get more comments about the future GM 3.x and above issue that we are going to hit. |
|
According to Claude Code's analysis:
The agent's proposal was above: adding num_threads(dt_get_num_threads()) to DT_OMP_FOR would fix it; a more detailed version:
|
d060937 to
6778d31
Compare
|
I rebased the branch onto current master. Claude Code added a new commit to cap the thread count and thus allocate memory according to the effective number of threads. We now have 3 commits: the original crash fix, the cap on the thread count, and the release note. Over to the bot: The new commit, With the cap, The cap needs #22475. Before that change, X-Trans raw denoise split its work by thread count, so the cap changed its output. The cap has no separate release note (the issue is about the bug fix in the first commit). Testing (Linux, 12 CPUs, Release build, CPU path):
The cap change has been reviewed by Codex CLI and Antigravity. |
|
@TurboGit : shall I create an issue for the GM >= 3 question? (#22438 (comment)) |
GraphicsMagick resets the main thread's OpenMP limit to the CPU count. With --threads set lower, darktable-cli can then run more threads than its per-thread buffers hold. Reapply the requested limit after Magick initialization. Fixes darktable-org#22436
6778d31 to
f1adb4a
Compare
|
Created #22567 to track the GMIC 3.x / 4.x issue. Rebased the code to fix the conflict in RELEASE_NOTES.md. |
GraphicsMagick resets the main thread's OpenMP limit to the CPU count. With --threads set lower, darktable-cli can then run more threads than its per-thread buffers hold. Reapply the requested limit after Magick initialization.
Summary
darktable-cli -t Nwith N below the CPU count crashed with heap corruption.Fix
Call
omp_set_num_threads(darktable.num_openmp_threads)again after theGraphicsMagick / ImageMagick init.
Referenced issue
Fixes #22436
Checklist
src/tests/integration/where the pixelpipe is touched, ordarktable-clias a headless smoke test._(), new preferences are registered indata/darktableconfig.xml.in.RELEASE_NOTES.mdentry was added (only needed if fixing an issue in a release). Do not reference GitHub issues.Test instructions
Tested locally: Linux, 12 CPUs, Release, GraphicsMagick 1.3.46,
--disable-opencl.xtransIV.rafwith-t 4: aborted before, exports now.-t 1,-t 8andno
-talso succeed.0068-rawdenoise-xtrans,0100-invert-xtransand
0145-lens-metadata-xtransIV-modversion-6, run with-t 4and withoutOMP_THREAD_LIMIT, crashed before and finish now.AI assistance
Fixed by Claude Code, reviewed by Codex CLI